#include "Head.h"
#include <QPaintEvent>
#include <QPainter>
#include <math.h>

Head::Head(QWidget* parent) :
	BaseItem(parent)
{
}

void Head::paintEvent(QPaintEvent*event)
{
	QPainter painter(this);

	painter.setRenderHint(QPainter::Antialiasing, true);

	if(_type._type == EmotionType::None){
		painter.setBrush(QBrush(_type.getColor()));
		painter.setPen(QPen(Qt::black, 4));
		painter.drawEllipse(2,2,_width-4,_height-4);
	}
	else{
		painter.drawPixmap(0,0,_width+4,_height+4,QPixmap(_type.getRes()));
		painter.setBrush(Qt::NoBrush);
		painter.setPen(QPen(Qt::black, 4));
		painter.drawEllipse(2,2,_width-4,_height-4);
	}

	painter.end();
}

bool Head::isInside(QPoint pt)
{
	// расчет идет при условии, что голова - круг, а не овал
	int R = _width/2;
	QPoint center = QPoint(_x0+_width/2, _y0+_height/2);

	int dist = sqrt((pt.x()-center.x())*(pt.x()-center.x()) + (pt.y()-center.y())*(pt.y()-center.y()));

	return dist < R;
}

void Head::applyEmotion(Emotion* emotion)
{
	if(emotion == NULL){
		_type = EmotionType();
	}
	else{
		_type = emotion->type();
	}
}
